package com.wave.wavesystem.business.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.wave.wavesystem.business.pojo.dto.user.UserLoginRequest;
import com.wave.wavesystem.business.pojo.dto.user.UserRegisterRequest;
import com.wave.wavesystem.business.pojo.vo.user.UserLoginVO;
import com.wave.wavesystem.business.service.AuthService;
import com.wave.wavesystem.common.constant.RequestConstant;
import wave.common.pojo.entity.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 夕下风波
 * @date 2025-02-02 22:31
 * @description
 */
@RestController
@RequestMapping("/auth")
@Tag(name = "权限操作接口")
public class AuthController {

    @Resource
    private AuthService authService;

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginRequest loginReq) {
        return Result.data(authService.login(loginReq));
    }

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody UserRegisterRequest registerReq) {
        return Result.data(authService.register(registerReq));
    }

    @Operation(summary = "用户退出登录")
    @PostMapping("/logout")
    public Result<Boolean> logout(HttpServletRequest request) {
        return Result.data(authService.logout(request));
    }

    @Operation(summary = "获取验证码")
    @GetMapping("/getVerificationCode")
    public Result<String> getVerificationCode(HttpServletRequest request) {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 50);
        request.getSession().setAttribute(RequestConstant.V_CODE, lineCaptcha.getCode());
        String imageBase64 = lineCaptcha.getImageBase64();
        return Result.data(imageBase64);
    }


}
